package hk.m4s.lr.repair.test.ui.user;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MyOrderTuiModel;
import hk.m4s.lr.repair.test.model.SmSCompnyModel;
import hk.m4s.lr.repair.test.ui.adapter.SmSCompanyliListAdapter;
import hk.m4s.lr.repair.test.utils.MoneyTool;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/5/9.
 * 商家处理换货请求
 */

public class HuanGoodsHandleActivity extends UeBaseActivity implements View.OnClickListener{
    private Context context;
    private Button pay_btn;
    private RelativeLayout click_pay_type,show_layout,click_jiangli_type;
    private ImageView order_shop_img;
    private EditText pay_order_num;
    private TextView pay_order_name,btn_cancle,btn_sure,order_shop_name,order_shop_price,order_shop_size,order_shop_num;
    private ListView jiangli_list;
    private List<SmSCompnyModel.ExpressBean>jiangliList=new ArrayList<>();
    private SmSCompanyliListAdapter jiangliListAdapter;
    private String jiangliName="";
    private MyOrderTuiModel.ListBean myOrderModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_order_huangoods_handle);
        hiddenFooter();
        setTitleText("换货详情");
        context=this;
        myOrderModel=(MyOrderTuiModel.ListBean)getIntent().getSerializableExtra("model");
        showGoBackBtn();
        findBaseView();
    }
    public void findBaseView(){
        pay_btn = (Button) findViewById(R.id.pay_ok_btn);
        click_pay_type= (RelativeLayout) findViewById(R.id.click_pay_type);
        show_layout= (RelativeLayout) findViewById(R.id.show_layout);
        pay_order_name= (TextView) findViewById(R.id.pay_order_name);
        btn_cancle= (TextView) findViewById(R.id.btn_cancle);
        order_shop_num= (TextView) findViewById(R.id.order_shop_num);
        order_shop_size= (TextView) findViewById(R.id.order_shop_size);
        order_shop_price= (TextView) findViewById(R.id.order_shop_price);
        order_shop_name= (TextView) findViewById(R.id.order_shop_name);
        pay_order_num=(EditText)findViewById(R.id.pay_order_num);
        btn_sure= (TextView) findViewById(R.id.btn_sure);
        order_shop_img=(ImageView)findViewById(R.id.order_shop_img);
        jiangli_list= (ListView) findViewById(R.id.jiangli_list);
        if(myOrderModel.getGoodsInfo().get(0).getGoods_name()!=null){
            order_shop_name.setText(myOrderModel.getGoodsInfo().get(0).getGoods_name());
        }
        if(myOrderModel.getGoodsInfo().get(0).getGoods_price()!=null){
            order_shop_price.setText(MoneyTool.getLocalMoney(myOrderModel.getGoodsInfo().get(0).getGoods_price()));
        }
        if(myOrderModel.getGoodsInfo().get(0).getGoods_color()!=null){
            order_shop_size.setText(myOrderModel.getGoodsInfo().get(0).getGoods_color());
        }
        if(myOrderModel.getGoodsInfo().get(0).getGoods_num()!=null){
            order_shop_num.setText("x"+myOrderModel.getGoodsInfo().get(0).getGoods_num());
        }
        try {
            Glide.with(context).load(myOrderModel.getGoodsInfo().get(0).getImg()).asBitmap().placeholder(R.mipmap.device_list_no_img).error(R.mipmap.device_list_no_img).into(order_shop_img);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pay_btn.setOnClickListener(this);
        click_pay_type.setOnClickListener(this);
        btn_cancle.setOnClickListener(this);
        btn_sure.setOnClickListener(this);
        jiangliListAdapter=new SmSCompanyliListAdapter(context,jiangliList);
        jiangli_list.setAdapter(jiangliListAdapter);
        //findAllExpress();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay_ok_btn:
                if(jiangliName.equals("")){
                    ToastUtil.toast(context,"请选择快递公司");
                    return;
                }else if(pay_order_num.getText().toString().equals("")){
                    ToastUtil.toast(context,"请填写快递单号");
                    return;
                }
               // updatereturnorder();
                break;
            case R.id.click_pay_type:
                show_layout.setVisibility(View.VISIBLE);
                pay_btn.setVisibility(View.GONE);
                break;
            case R.id.btn_cancle:
                show_layout.setVisibility(View.GONE);
                pay_btn.setVisibility(View.VISIBLE);
                jiangliName="";
                break;
            case R.id.btn_sure:
                pay_btn.setVisibility(View.VISIBLE);
                show_layout.setVisibility(View.GONE);
                for(SmSCompnyModel.ExpressBean model:jiangliList){
                    if(model.getState().equals("1")){
                        jiangliName=model.getName();
                    }
                }
                pay_order_name.setText(jiangliName);
                break;
        }

    }
//    public void findAllExpress() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        MyService.findAllExpress(context, parmas, new ResponseCallback<SmSCompnyModel>() {
//            @Override
//            public void onSuccess(SmSCompnyModel userinfoModel) {
//                if(userinfoModel!=null) {
//                    if(userinfoModel.getExpress()!=null){
//                        jiangliList.addAll(userinfoModel.getExpress());
//                        jiangliListAdapter.notifyDataSetChanged();
//                    }
//                }else{
//
//                }
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//    }
//    public void updatereturnorder() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("returnid",myOrderModel.getId());
//        parmas.put("expressage_number",pay_order_num.getText().toString());
//        parmas.put("expressage",jiangliName);
//        MyService.updatereturnorder(context, parmas, new ResponseCallback<SmSCompnyModel>() {
//            @Override
//            public void onSuccess(SmSCompnyModel userinfoModel) {
//                finish();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//    }
}
